<template>
  <div class="form-container">
    <el-form 
      :model="formData" 
      :rules="rules" 
      ref="formRef" 
      label-width="100px" 
      class="demo-ruleForm"
    >
      <el-form-item label="ID">
        <el-input v-model="formData.id" readonly />
      </el-form-item>
      
      <el-form-item label="学号" prop="number">
        <el-input v-model="formData.number" />
      </el-form-item>
      
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="formData.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="宿舍" prop="dormitoryId">
        <el-select 
          v-model="formData.dormitoryId" 
          placeholder="请选择宿舍"
          style="width: 100%"
        >
          <el-option 
            v-for="item in dormitoryList" 
            :key="item.id"
            :label="item.name" 
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即修改</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import studentApi from '@/api/student'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const formRef = ref(null)

const formData = ref({
  id: '',
  number: '',
  name: '',
  gender: '男',
  dormitoryId: ''
})

const dormitoryList = ref([])

const rules = {
  number: [
    { required: true, message: '请输入学号', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  dormitoryId: [
    { required: true, message: '请选择宿舍', trigger: 'change' }
  ]
}

// 提交表单
const submitForm = async () => {
  try {
    await formRef.value.validate()
    const res = await studentApi.updateStudent(formData.value)
    
    if (res.data.code === 0) {
      await ElMessageBox.alert(`${formData.value.name}修改成功`, {
        confirmButtonText: '确定',
        callback: () => {
          router.push('/studentManager')
        }
      })
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('修改失败: ' + (error.response?.data?.message || error.message))
    }
  }
}

// 重置表单
const resetForm = () => {
  formRef.value.resetFields()
}

// 初始化数据
onMounted(async () => {
  try {
    // 获取宿舍列表
    const dormitoriesRes = await studentApi.getAvailableDormitories()
    dormitoryList.value = dormitoriesRes.data.data
    
    // 获取学生信息
    const studentRes = await studentApi.getStudentById(route.query.id)
    formData.value = studentRes.data.data
  } catch (error) {
    ElMessage.error('初始化数据失败: ' + error.message)
    router.push('/studentManager')
  }
})
</script>

<style scoped>
.form-container {
  margin: 60px auto;
  width: 500px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.demo-ruleForm {
  margin-top: 20px;
}

.el-radio-group {
  width: 100%;
}
</style>